package dao

import (
	"enotify/internal/xys/notify/model"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/xormplus/xorm"
)

type INotify interface {
	AddLog(log model.Log) (id int64,err error)
	UpdateLog(id int64, log model.Log) error
	GetLog(id int64) (model.Log,bool,error)
	GetEvent(id int64)(model.Event,bool,error)
	GetWxTemplate(id int) (t model.WxTemplate,find bool,err error)
}
type Config struct {
	MySql *MySql
}

type Dao struct {
	conf *Config
	engine *xorm.Engine
}
type option func(s *Config)
func NewDao(mysql *MySql,opts... option) (*Dao,error){
	cofing := &Config{
		MySql: mysql,
	}

	for _,opt := range opts  {
		opt(cofing)
	}
	var err error
	d := &Dao{conf: cofing}
	dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
		mysql.UserName,
		mysql.Password,
		mysql.Addr,
		mysql.Db)
	d.engine, err = xorm.NewEngine("mysql",dataSourceName)
	if err != nil {
		return nil,err

	}
	d.engine.ShowSQL(true)
	return d,err
}

